import requests
from utils import url_manager
from bs4 import BeautifulSoup
from utils import province_dictionary

"""
:param province      : 省份简称
:param url           : 该省会的url
:param province_cn   : 省份中文名称
"""


class CrawlUtil:

    def __init__(self, province):
        self.url = province_dictionary.get_url(province)
        self.province = province

    # def crawl_today_temperature(self):
    #     self.__crawl_url()
    #     self.__get_today_max_min_temperature()

    def crawl_seven_day_temperature(self):
        self.__crawl_url()
        return self.__get_seven_day_max_min_temperature()

    """
    爬取各个城市的url并保存在redis
    """

    def __crawl_url(self):
        url_m = url_manager.UrlManager()

        resp = requests.get(self.url)
        resp.encoding = "utf-8"

        soup = BeautifulSoup(resp.text, "html.parser")
        cities_info_set = soup.find("div", id="cityPosition").find_all("ul", class_="dropdown-menu station-select")
        for city_info_set in cities_info_set:
            cities_info = city_info_set.find_all("a")
            for city_info in cities_info:
                url = city_info["href"]
                name = city_info.get_text()
                str2save = url + '`' + name
                url_m.add_new_url(self.province, str2save)

    """
    将保存在redis的url取出并爬取天气数据
    """

    def __get_seven_day_max_min_temperature(self):
        url_m = url_manager.UrlManager()
        # 该字典用于存放市名:温度列表
        dict_of_temp = {}
        # 遍历redis中的url`市名
        while url_m.has_url(self.province):
            # 该列表用于存放温度元组
            list_of_temp = []
            str2get = url_m.get_url(self.province)
            index = str2get.index('`')
            # 取出url
            url = str2get[:index]
            # 取出市名
            city_name = str2get[index + 1:]
            # 向url发请求(首先拼接前缀url)
            resp = requests.get("https://weather.cma.cn" + url)
            resp.encoding = "utf-8"
            # dom解析
            soup = BeautifulSoup(resp.text, "html.parser")
            today_info = soup.find("div", id="dayList").find("div", class_="pull-left day actived")
            other_days_info = soup.find("div", id="dayList").find_all("div", class_="pull-left day")
            minTempToday = (today_info.find("div", class_="low")).get_text().strip()
            maxTempToday = (today_info.find("div", class_="high")).get_text().strip()
            todayTemp = (maxTempToday, minTempToday)
            # 提取出今日温度，并生成元组：(最高温度,最低温度)，放入温度列表中
            list_of_temp.append(todayTemp)
            print(f"{city_name}：\n今日最高温度：{maxTempToday}；最低温度：{minTempToday}")
            i = 0
            for other_day_info in other_days_info:
                # 解析温度
                minTemp = (other_day_info.find("div", class_="low")).get_text().strip()
                maxTemp = (other_day_info.find("div", class_="high")).get_text().strip()
                this_dayTemp = (maxTemp, minTemp)
                # 提取出后i日温度，并生成元组(最高温度,最低温度)，放入温度列表中
                list_of_temp.append(this_dayTemp)
                i = i + 1
                print(f"后{i}日最高温度：{maxTemp}；最低温度：{minTemp}")
            # 得到该市的所有的最高最低温度后，放入字典，key为市名，value为温度列表
            dict_of_temp[city_name] = list_of_temp
        # 返回字典
        return dict_of_temp
